package com.workerhelping.controller;

import com.workerhelping.dto.RecordIdDTO;
import com.workerhelping.dto.StuckReasonIdsDTO;
import com.workerhelping.dto.WorkerIdDTO;
import com.workerhelping.po.Record;
import com.workerhelping.po.StuckReason;
import com.workerhelping.result.Result;
import com.workerhelping.service.*;
import com.workerhelping.vo.WorkerPortalVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private WorkerPortalVoService workerPortalVoService;

    @Autowired
    private RecordStuckReasonIdRelationService recordStuckReasonIdRelationService;

    @Autowired
    private StuckReasonService stuckReasonService;

    @Autowired
    private StuckReasonIdVOService stuckReasonIdVOService;

    /**
     * 插入档案
     *
     * @param record
     * @return
     */
    @RequestMapping("/insert")
    public Result<Integer> insertRecord(@RequestBody Record record) {
        Integer id = recordService.insertRecord(record);
        return Result.ok(id);
    }

    /**
     * 根据id查找职工主页视图对象
     *
     * @param workerIdDTO
     * @return
     */
    @PostMapping("/getRecordWithWorkerById")
    public Result<WorkerPortalVO> getRecordWithWorkerById(@RequestBody WorkerIdDTO workerIdDTO) {
        try {
            WorkerPortalVO workerPortalVO = workerPortalVoService.getRecordWithWorkerById(workerIdDTO.getId());
            return Result.ok(workerPortalVO);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }


    /**
     * 查询所有致困信息
     *
     * @return
     */
    @PostMapping("/getAllStuckReasons")
    public Result<List<StuckReason>> getAllStuckReasons() {
        try {
            List<StuckReason> list = stuckReasonService.list();
            return Result.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 保存档案与至困原因的关联关系
     *
     * @param stuckReasonIdsDTO
     * @return
     */
    @PostMapping("/setRecordStuckReasonsRelation")
    public Result<String> setRecordStuckReasonsRelation(@RequestBody StuckReasonIdsDTO stuckReasonIdsDTO) {
        try {
            recordStuckReasonIdRelationService.setRecordStuckReasonsRelation(
                    stuckReasonIdsDTO.getStuckReasonIds(),
                    stuckReasonIdsDTO.getId());
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 根据WorkerId查询档案
     * @param workerIdDTO
     * @return
     */
    @PostMapping("/getRecordByWorkerId")
    public Result<Record> getRecordByWorkerId(@RequestBody WorkerIdDTO workerIdDTO) {
        try {
            Record record = recordService.getRecordByWorkerId(workerIdDTO.getId());
            return Result.ok(record);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 根据RecordId查询相关联的致困原因id
     * @param recordIdDTO
     * @return
     */
    @PostMapping("/getRecordStuckReasonByRecordId")
    public Result<List<Integer>> getRecordStuckReasonByRecordId(@RequestBody RecordIdDTO recordIdDTO) {
        try {
            List<Integer> ids = stuckReasonIdVOService.getRecordStuckReasonByRecordId(recordIdDTO.getId());
            return Result.ok(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 申请更新档案信息
     * @param record
     * @return
     */
    @PostMapping("/updateRecordByRecordId")
    public Result<String> updateRecordByRecordId(@RequestBody Record record) {
        try {
            recordService.updateRecordByRecordId(record);
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 删除档案与致困信息关联表中信息
     * @param recordIdDTO
     * @return
     */
    @PostMapping("/deleteRecordStuckReasonByRecordId")
    public Result<String> deleteRecordStuckReasonByRecordId(@RequestBody RecordIdDTO recordIdDTO) {
        try {
            recordStuckReasonIdRelationService.deleteRecordStuckReasonsRelation(recordIdDTO.getId());
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }

    }

    /**
     * 根据id删除档案信息
     * @param recordIdDTO
     * @return
     */
    @PostMapping("/deleteRecordById")
    public Result<String> deleteRecordById(@RequestBody RecordIdDTO recordIdDTO) {
        try {
            boolean isDelete = recordService.removeById(recordIdDTO.getId());
            if (isDelete)
                return Result.ok();
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
